<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router' // 导入useRouter
// 获取路由实例
const router = useRouter()
// 表单引用
const loginFormRef = ref<FormInstance>()

// 加载状态
const isLoading = ref<boolean>(false)

// 自动完成设置
const isAutocomplete = ref<boolean>(true)

// 登录表单数据
const loginForm = reactive({
  username: 'admin',
  password: '12345678',
})

// 表单验证规则
const loginRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
  ],
})

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    // 表单验证
    await loginFormRef.value.validate()

    // 显示加载状态
    isLoading.value = true

    // 模拟登录请求
    setTimeout(() => {
      // 实际应用中替换为真实登录API调用
      console.log('登录信息:', { ...loginForm })
      ElMessage.success('登录成功')

      // 登录成功后的操作，如路由跳转
      router.push('/visualization')

      isLoading.value = false
    }, 1000)
  } catch (error) {
    console.error('表单验证失败:', error)
    isLoading.value = false
  }
}

// 处理忘记密码
const handleForgotPassword = () => {
  ElMessage({
    message: '忘记密码功能即将上线',
    type: 'warning',
    duration: 3000,
  })

  // 实际应用中可以跳转到密码重置页面
  // router.push('/forgot-password');
}

// 处理第三方登录
const handleSocialLogin = (type: string) => {
  // // 模拟第三方登录请求
  // setTimeout(() => {
  //   ElMessage.success(`正在使用${getSocialName(type)}登录`)
  //   console.log(`使用${type}登录`)
  //   isLoading.value = false
  // }, 800)
  if (type === 'qq') {
    ElMessage({
      message: '暂不支持QQ登录',
      type: 'warning',
      duration: 3000,
    })
  }
  if (type === 'wechat') {
    ElMessage({
      message: '暂不支持微信登录',
      type: 'warning',
      duration: 3000,
    })
  }
  if (type === 'weibo') {
    ElMessage({
      message: '暂不支持微博登录',
      type: 'warning',
      duration: 3000,
    })
  }
}

// 获取第三方平台名称
// const getSocialName = (type: string): string => {
//   const names: Record<string, string> = {
//     // 添加索引签名
//     qq: 'QQ',
//     wechat: '微信',
//     alipay: '微博',
//   }
//   return names[type] || type
// }
</script>
<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="bg-decoration"></div>

    <!-- 登录容器 -->
    <div class="login-container">
      <div class="login-card">
        <!-- 登录标题 -->
        <div class="login-header">
          <h2 class="login-title">账号登录</h2>
          <p class="login-subtitle">login to your account</p>
        </div>

        <!-- 登录表单 -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
          <!-- 账号输入 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="User"
              :autocomplete="isAutocomplete ? 'on' : 'off'"
            ></el-input>
          </el-form-item>

          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              :autocomplete="isAutocomplete ? 'on' : 'off'"
              show-password
            ></el-input>
          </el-form-item>

          <!-- 忘记密码 -->
          <div class="forgot-password">
            <el-link class="forgot-link" type="primary" @click="handleForgotPassword">忘记密码?</el-link>
          </div>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              @click="handleLogin"
              :loading="isLoading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 其他登录方式 -->
        <div class="other-login">
          <div class="separator">
            <span>其他登录方式</span>
          </div>

          <div class="social-login">
            <el-icon @click="handleSocialLogin('qq')" class="social-icon qq-icon">
              <img src="../../../pictures/icons8-tencent-qq-96.png" class="icons8-wechat" />
            </el-icon>

            <el-icon @click="handleSocialLogin('wechat')" class="social-icon wechat-icon">
              <img src="../../../pictures/icons8-wechat-500.svg" class="icons8-wechat" />
            </el-icon>

            <el-icon @click="handleSocialLogin('weibo')" class="social-icon alipay-icon">
              <img src="../../../pictures/icons8-weibo.svg" class="icons8-wechat" />
            </el-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 基础样式 */
.login-page {
  margin-top: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;
}

/* 登录容器 */
.login-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

/* 登录标题 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: #606266;
}

/* 登录表单 */
.login-form {
  margin-bottom: 15px;
}

.el-input {
  height: 48px;
  border-radius: 8px;
}

.el-input__wrapper {
  border-radius: 8px;
  height: 48px;
}

/* 忘记密码 */
.forgot-password {
  text-align: right;
  /* color: #000; */
  margin-bottom: 20px;
  text-decoration: none !important; /* 去除默认下划线 */
}

/* 使用深度选择器穿透组件样式 */
::v-deep .forgot-link {
  color: #000 !important;
  font-weight: 500;
  text-decoration: none !important;
}

::v-deep .forgot-link:hover {
  color: #333 !important;
  text-decoration: none !important;
}

/* 额外处理可能存在的伪元素下划线 */
::v-deep .forgot-link:hover::after {
  display: none !important;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  background-color: rgb(156, 12, 19);
  border-color: rgb(156, 12, 19);
  transition: all 0.3s ease;
}

.login-button:hover,
.login-button:focus {
  background-color: #7d0a11;
  border-color: #7d0a11;
}

/* 其他登录方式 */
.other-login {
  margin-top: 30px;
}

/* 分隔线 */
.separator {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.separator::before,
.separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: #e5e7eb;
}

.separator span {
  padding: 0 15px;
  font-size: 14px;
  color: #909399;
}

.icons8-wechat {
  width: 30px;
  height: 30px;
}

/* 社交登录图标 */
.social-login {
  display: flex;
  justify-content: center;
  gap: 30px;
}

.social-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 5px;
}

.social-icon:hover {
  transform: scale(1.15);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.qq-icon {
  color: #12b7f5;
}

.wechat-icon {
  color: #07c160;
}

.alipay-icon {
  color: #1677ff;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }

  .social-login {
    gap: 20px;
  }

  .social-icon {
    width: 36px;
    height: 36px;
  }
}
</style>
